package com.sqnu.dashe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqnu.dashe.common.CustomException;
import com.sqnu.dashe.dto.DishDto;
import com.sqnu.dashe.entity.Category;
import com.sqnu.dashe.entity.Dish;
import com.sqnu.dashe.entity.DishFlavor;
import com.sqnu.dashe.mapper.DishMapper;
import com.sqnu.dashe.service.CategoryService;
import com.sqnu.dashe.service.DishFlavorService;
import com.sqnu.dashe.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mmy
 * @create 2023-06-08 20:45
 * @description
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishService dishService;
    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Transactional
    public void saveWithFlavo(DishDto dishDto) {
        //保存信息到菜品表dish
        this.save(dishDto);
        Long dishId = dishDto.getId();//菜品id
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().map((item -> {
            item.setDishId(dishId);
            return item;
        })).collect(Collectors.toList());
        //保存菜品口味数据到dish_flavor
        dishFlavorService.saveBatch(flavors);

    }

    @Override
    public DishDto getByIdWithFlavo(Long id) {
        DishDto dishDto = new DishDto();
        Dish byId = this.getById(id);
        BeanUtils.copyProperties(byId,dishDto);

        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,byId.getId());
        List<DishFlavor> list = dishFlavorService.list(lambdaQueryWrapper);

        dishDto.setFlavors(list);
        return dishDto;
    }

    @Override
    @Transactional
    public void MyUpdateById(DishDto dishDto) {
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().map((item -> {
            item.setDishId(dishDto.getId());
            return item;
        })).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
        this.updateById(dishDto);
    }

    @Override
    public void deleteDish(List<Long> ids) {
        //判断当前菜品的状态
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(Dish::getId , ids);
        dishLambdaQueryWrapper.eq(Dish::getStatus, 1);
        int count = dishService.count(dishLambdaQueryWrapper);

        if (count>0){
            //如果不能删除，就抛出异常
            throw new CustomException("菜品为启售状态，无法删除");
        }

        //如果能删除，删除菜品信息
        this.removeByIds(ids);

        //删除对应的口味信息
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.in(DishFlavor::getDishId,ids);
        //执行删除口味
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
    }



}
